package com.cat.dataStructure02;

class BrowserHistory {
    static int N = 5001, top, n;
    static String[] stack = new String[N];

    public BrowserHistory(String homepage) {
        top = n = -1;
        stack[++top] = homepage;
        n++;
    }

    public void visit(String url) {
        stack[++top] = url; //
        n = top;
    }

    public String back(int steps) {
        if (top >= steps) {
            top -= steps;
            return stack[top];
        } else {
            top = 0;
            return stack[top];
        }
    }

    public String forward(int steps) {
        if (top + steps <= n) {
            top += steps;
            return stack[top];
        } else {
            top = n;
            return stack[top];
        }
    }
}